﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataCoreTechnologies.SqlServer
{
    internal static class TSql
    {
        public static List<string> GetDDLTokens(string ddl)
        {
            List<string> tokens = new List<string>();
            ddl = ddl.Replace("\t", " ");
            for (int i = 0; i < ddl.Length; i++)
            {
                //Block comments
                if (i < ddl.Length - 1 && ddl.Substring(i, 2) == @"/*")
                {
                    string code = ddl.Substring(i, 2);
                    i += 2;
                    while (i < ddl.Length - 1 && ddl.Substring(i, 2) != @"*/")
                    {
                        code += ddl.Substring(i, 1);
                        i++;
                    }
                    code += ddl.Substring(i, 2);
                    i += 2;
                    i--;
                    tokens.Add(code);
                }
                //Single line comments
                else if (i < ddl.Length - 1 && ddl.Substring(i, 2) == "--")
                {
                    string code = ddl.Substring(i, 2);
                    i += 2;
                    while (i < ddl.Length - 1 && ddl.Substring(i, 2) != "\r\n")
                    {
                        code += ddl.Substring(i, 1);
                        i++;
                    }
                    if (i + 1 == ddl.Length)
                        code += ddl.Substring(i, 1);
                    else
                        i--;
                    tokens.Add(code);
                }
                //White space
                else if (ddl.Substring(i, 1) == " ")
                {
                    string spaces = "";
                    while (i < ddl.Length && ddl.Substring(i, 1) == " ")
                    {
                        spaces += " ";
                        i++;
                    }
                    i--;
                    tokens.Add(spaces);
                }
                //Apprentheses
                else if (i < ddl.Length && (ddl.Substring(i, 1) == "("
                || ddl.Substring(i, 1) == ")"
                || ddl.Substring(i, 1) == ","
                || ddl.Substring(i, 1) == ";"))
                {
                    tokens.Add(ddl.Substring(i, 1));
                }
                //Brackets
                else if (i < ddl.Length && ddl.Substring(i, 1) == "[")
                {
                    string code = "";
                    while (ddl.Substring(i, 1) != "]")
                    {
                        code += ddl.Substring(i, 1);
                        i++;
                    }
                    code += ddl.Substring(i, 1);
                    tokens.Add(code);
                }
                //Quoted identifiers
                else if (i < ddl.Length && ddl.Substring(i, 1) == "'")
                {
                    string code = ddl.Substring(i, 1);
                    i++;
                    while (ddl.Substring(i, 1) != "'")
                    {
                        code += ddl.Substring(i, 1);
                        i++;
                    }
                    code += ddl.Substring(i, 1);
                    tokens.Add(code);
                }
                else if (i < ddl.Length - 1 && ddl.Substring(i, 2) == "\r\n")
                {
                    tokens.Add("\r\n");
                    i++;
                }
                else
                {
                    string code = "";
                    while (i < ddl.Length
                    && ddl.Substring(i, 1) != " "
                    && ddl.Substring(i, 1) != "["
                    && ddl.Substring(i, 1) != "'"
                    && ddl.Substring(i, 1) != "("
                    && ddl.Substring(i, 1) != ")"
                    && ddl.Substring(i, 1) != ","
                    && ddl.Substring(i, 1) != ";")
                    {
                        if (i < ddl.Length - 1
                        && (ddl.Substring(i, 2) == "--"
                        || ddl.Substring(i, 2) == "\r\n"
                        || ddl.Substring(i, 2) == @"/*"))
                        {
                            break;
                        }
                        else
                        {
                            code += (ddl.Substring(i, 1));
                            i++;
                        }
                    }
                    tokens.Add(code);
                    i--;
                }
            }
            return tokens;
        }
    }
}
